//
//  MessageViewController.m
//  ivcs_ios_demo
//
//  Created by zealcomm user on 2020/10/27.
//  Copyright © 2020 zealcomm user. All rights reserved.
//

#import "MessageViewController.h"

@interface MessageViewController ()<CCSClientDelegate,UIDocumentPickerDelegate>

@property(copy,nonatomic)NSMutableString *receiveMessage;

@end

@implementation MessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.ivcsSdk.ccsClient.delegate = self;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)sendMsg:(id)sender {
    [self.ivcsSdk sendTextMessage:self.messageTextField.text toRoles:@"agents" onSuccess:^{
        NSLog(@"..send success");
    } onFailure:^(NSError * _Nonnull error) {
        NSLog(@"xxx..%@",error);
    }];
}


/// 发送测试的照片给x坐席
/// @param sender <#sender description#>
- (IBAction)sendImg:(id)sender {
    UIImage *image = [UIImage imageNamed:@"test.jpg"];
    NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
    [self.ivcsSdk shareResourceWithType:@"pic" uploadUrl:@"https://ivcs-demo.zealcomm.cn:3094/v1/upload" fileData:imageData fileName:@"test.jpg" mimeType:@"image/jpg" toRoles:@"agents" onSuccess:^{
        NSLog(@"2223success");
    } onFailure:^(NSError * _Nonnull error) {
        NSLog(@"..11error%@",error);
    }];
    
    //发送url资源给坐席
    [self.ivcsSdk shareResourceWithType:@"pic" resourceUrl:@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603891137312&di=427368f2c88eee52902a86ffcd1fea4d&imgtype=0&src=http%3A%2F%2Fa0.att.hudong.com%2F56%2F12%2F01300000164151121576126282411.jpg" resourceName:@"test.png" toRoles:@"agents" onSuccess:^{
         NSLog(@"success");
    } onFailure:^(NSError * _Nonnull error) {
        NSLog(@"..error%@",error);
    }];
    
}

- (IBAction)sendFile:(id)sender {
     NSArray *documentTypes = @[@"com.microsoft.word.doc",@"com.microsoft.word.docx",@"com.adobe.pdf",@"com.microsoft.powerpoint.ppt",@"public.content",@"public.text",@"public.source-code",@"public.image",@"public.audiovisual-content",@"com.apple.keynote.key",@"com.microsoft.excel.xls",@"public.archive",@"public.text",@"public.data",@"public.audio",@"public.movie"];
        
        UIDocumentPickerViewController *vc =[[UIDocumentPickerViewController alloc] initWithDocumentTypes:documentTypes inMode:UIDocumentPickerModeOpen];
        
        vc.delegate = self;
        
        vc.modalPresentationStyle = UIModalPresentationFormSheet;
        [self presentViewController:vc animated:false completion:nil];
        
    }

-(void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray<NSURL *> *)urls{
        NSLog(@"...%@",urls);
        //获取授权
        BOOL fileUrlAuthozied = [urls.firstObject startAccessingSecurityScopedResource];
        if (fileUrlAuthozied) {
               //通过文件协调工具来得到新的文件地址，以此得到文件保护功能
                NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] init];
                 NSError *error;
                 [fileCoordinator coordinateReadingItemAtURL:urls.firstObject options:0 error:&error byAccessor:^(NSURL *newURL) {
                //读取文件
                 NSString *fileName = [newURL lastPathComponent];
                 NSError *error = nil;
                 NSData *fileData = [NSData dataWithContentsOfURL:newURL options:NSDataReadingMappedIfSafe error:&error];
                 if (error) {
                    //读取出错
                     NSLog(@"..123error%@",error);
                 } else {
                     [self.ivcsSdk shareResourceWithType:@"doc" uploadUrl:@"https://ivcs-demo.zealcomm.cn:3094/v1/upload" fileData:fileData fileName:fileName mimeType:@"application/vnd.ms-powerpoint" toRoles:@"agents" onSuccess:^{
                          NSLog(@"11...success");
                     } onFailure:^(NSError * _Nonnull error) {
                         NSLog(@"..23.%@",error);
                     }];
                 }
                [self dismissViewControllerAnimated:YES completion:NULL];
                 }];
                [urls.firstObject stopAccessingSecurityScopedResource];
        } else {
         //授权失败
         }
 }
- (void)OnResourceData:(id _Nullable)data {
    
    NSLog(@"OnResourceData,,%@",data);
}

- (void)agentVideoReadyToTalk {
    
}

- (void)ccsErrorWithCode:(IVCSSDKErrorCode)code andMessage:(NSString * _Nullable)message {
    
}

- (void)onCustomMessage:(id _Nullable)message {
    
}

- (void)onDrop:(NSDictionary * _Nonnull)message {
    
}

- (void)onFormData:(id _Nullable)data {
    
}

- (void)onHandSignatureRequest {
    
}

- (void)onHoldOn {
    
}

- (void)onJoinSession:(NSDictionary * _Nonnull)personInfo {
    
}

- (void)onPhotoGuideBox {
    
}

- (void)onQuitSession:(NSDictionary * _Nonnull)personInfo {
    
}

- (void)onScreenSnapshotRequest {
    
}

- (void)onTextMessage:(id _Nullable)message {
    NSLog(@"ontext message,,%@",message);
}

- (void)requestSessionProgress:(NSDictionary * _Nonnull)progress {
    
}



@end
